
//
//  ChatMessageListCell.m
//  ChildHoodStemp
//
//  Created by 日东 罗 on 13-8-30.
//
//

#import "ChatMessageListCell.h"
#import<QuartzCore/QuartzCore.h>
#import "MutiltimediaView.h"
#import "Snsp.pb.h"
#import "UIUtil.h"
#import "MicroDef.h"
#import "UIButton+WebCache.h"
#import "NSString+CBExtension.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "CGroupModel.h"
#import "CGroupMgr.h"
//头像大小
#define HEAD_SIZE [UIImage imageNamed:@"110-110"].size.width
//间距
#define INSETS 10.0f

#define CELL_HEIGHT self.contentView.frame.size.height


@implementation ChatMessageListCell
-(void)setHeadImage
{
    
}
-(void)dealloc
{
        }

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andWidth:(CGFloat)width
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        _width = width;
        _userHead =[[UIButton alloc]initWithFrame:CGRectZero];
        _userHead.adjustsImageWhenHighlighted = NO;
        _userHead.userInteractionEnabled = NO;
        
        
        [_userHead setFrame:CGRectMake(INSETS, (CELL_HEIGHT-HEAD_SIZE)/2,HEAD_SIZE , HEAD_SIZE)];
        
        _userNickname=[[UILabel alloc]initWithFrame:CGRectZero];
        _messageConent=[[UILabel alloc]initWithFrame:CGRectZero];
        _noticeLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _messageStatusimage = [[UIImageView alloc]initWithFrame:CGRectZero];
        _timeLable=[[UILabel alloc]initWithFrame:CGRectZero];
        //        _cellBkg= //[[UIView alloc] initWithFrame:CGRectZero];
        //_cellBkg.backgroundColor = [UIColor whiteColor];
        //        [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MessageListCell_bg"]];
        _bageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabbar_badge"]];
        _bageNumber=[[UILabel alloc]initWithFrame:CGRectZero];
        _stateFaileview = [[UIImageView alloc]initWithFrame:CGRectZero];
        //提醒图标
        _remindview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"alarm---bell"]];
        [_userHead.layer setCornerRadius:5.0f];
        [_userHead.layer setMasksToBounds:YES];
        
        [_userNickname setFont:[UIFont boldSystemFontOfSize:16]];
        [_userNickname setTextColor:[UIUtil colorWithHexString:@"#595757" withAlpha:1]];
        [_userNickname setBackgroundColor:[UIColor clearColor]];
        [_messageConent setFont:[UIFont systemFontOfSize:14]];
        [_messageConent setTextColor:[UIColor lightGrayColor]];
        [_messageConent setBackgroundColor:[UIColor clearColor]];
        
        [_timeLable setTextColor:[UIColor lightGrayColor]];
        [_timeLable setFont:[UIFont systemFontOfSize:12]];
        [_timeLable setTextAlignment:NSTextAlignmentRight];
        [_timeLable setBackgroundColor:[UIColor clearColor]];
        
        [_bageNumber setBackgroundColor:[UIColor clearColor]];
        [_bageNumber setTextAlignment:NSTextAlignmentCenter];
        [_bageNumber setTextColor:[UIColor whiteColor]];
        [_bageNumber setFont:[UIFont boldSystemFontOfSize:10]];
        
        [_noticeLable setTextColor:[UIColor colorWithRed:.89 green:.56 blue:.22 alpha:1]];
        [_noticeLable setFont:[UIFont systemFontOfSize:14]];
        [_noticeLable setBackgroundColor:[UIColor clearColor]];
        
        //        [_messageStatusLabel setTextColor:[UIColor colorWithRed:.89 green:.56 blue:.22 alpha:1]];
        //        [_messageStatusLabel setFont:[UIFont systemFontOfSize:14]];
        //        [_messageStatusLabel setBackgroundColor:[UIColor clearColor]];
        
        [self setBackgroundView:_cellBkg];
        
        [self.contentView addSubview:_userHead];
        [self.contentView addSubview:_userNickname];
        [self.contentView addSubview:_messageConent];
        [self.contentView addSubview:_timeLable];
        [self.contentView addSubview:_bageView];
        [self.contentView addSubview:_remindview];
        [self.contentView addSubview:_noticeLable];
        [self.contentView addSubview:_messageStatusimage];
        [_bageView addSubview:_bageNumber];
        UIView *shortLine = [[UIView alloc] init];
        shortLine.backgroundColor = RGBACOLORWithAlpha(212, 212, 212, 0.8f);
        [self.contentView addSubview:shortLine];
        self.shortLine = shortLine;
        
        UIView *longLine = [[UIView alloc] init];
        longLine.backgroundColor = RGBACOLORWithAlpha(212, 212, 212, 0.8f);
        [self.contentView addSubview:longLine];
        self.longLine = longLine;
        
        
        self.backgroundColor = [UIColor whiteColor];//[UIUtil colorWithHexString:@"#f9f5eb" withAlpha:1.0];
        
        //        self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
        //        self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [_userHead setFrame:CGRectMake(INSETS, (CELL_HEIGHT-HEAD_SIZE)/2,HEAD_SIZE , HEAD_SIZE)];
    [_userNickname setFrame:CGRectMake(2*INSETS+HEAD_SIZE, (CELL_HEIGHT-HEAD_SIZE)/2, (HARDWARE_SCREEN_WIDTH -HEAD_SIZE-INSETS*3) - 80.0f, (CELL_HEIGHT-3*INSETS)/2)];
    //[_messageConent setFrame:CGRectMake(2*INSETS+HEAD_SIZE, (CELL_HEIGHT-HEAD_SIZE)/2+_userNickname.frame.size.height+INSETS-6, (CELL_WIDTH-HEAD_SIZE-INSETS*3)-14, (CELL_HEIGHT-3*INSETS)/2 -8)];
    [_timeLable setFrame:CGRectMake(_width-80-5, (CELL_HEIGHT-HEAD_SIZE)/2, 80, (CELL_HEIGHT-3*INSETS)/2-4)];
    [_bageNumber setFrame:CGRectMake(0,0, _bageView.image.size.width, _bageView.image.size.height)];
    [_bageView setFrame:CGRectMake(self.frame.size.width - 10 - _bageView.image.size.width,self.frame.size.height - 15 - _bageView.image.size.height, _bageView.image.size.width, _bageView.image.size.height)];
    [_remindview setFrame:CGRectMake(self.frame.size.width - 10 - _bageView.image.size.width,self.frame.size.height - 15 - _bageView.image.size.height, _bageView.image.size.width, _bageView.image.size.height)];
    _cellBkg.frame=self.contentView.frame;
    //[_noticeLable setFrame:CGRectMake(2*INSETS+HEAD_SIZE, (CELL_HEIGHT-HEAD_SIZE)/2+_userNickname.frame.size.height+INSETS-6, 0, (CELL_HEIGHT-3*INSETS)/2 -8)];
    
    
    if ([_noticeLable.text isEqualToString:@"[通知]"]) {
        [_noticeLable setFrame:CGRectMake(2*INSETS+HEAD_SIZE, (CELL_HEIGHT-HEAD_SIZE)/2+_userNickname.frame.size.height+INSETS-6, 37, (CELL_HEIGHT-3*INSETS)/2 -8)];
        //_noticeLable.text = @"[通知]";
        
        [_messageConent setFrame:CGRectMake(2*INSETS+HEAD_SIZE+37, (CELL_HEIGHT-HEAD_SIZE)/2+_userNickname.frame.size.height+INSETS + 3, _bageView.frame.origin.x - 5 - 2*INSETS -HEAD_SIZE - 37, (CELL_HEIGHT-3*INSETS)/2)];
        return;
    }else{
        [_messageConent setFrame:CGRectMake(2*INSETS+HEAD_SIZE, CELL_HEIGHT - (CELL_HEIGHT-HEAD_SIZE)/2 - Font14.lineHeight - 3, _bageView.frame.origin.x - 5 - 2*INSETS -HEAD_SIZE, Font14.lineHeight)];
        [_noticeLable setFrame:CGRectMake(2*INSETS+HEAD_SIZE, (CELL_HEIGHT-HEAD_SIZE)/2+_userNickname.frame.size.height+INSETS, 0, (CELL_HEIGHT-3*INSETS)/2)];
        
    }
    if ([_strState isEqualToString:@"发送中"]) {
        [_messageStatusimage setFrame:CGRectMake(2*INSETS+HEAD_SIZE, CELL_HEIGHT - (CELL_HEIGHT-HEAD_SIZE)/2 - Font14.lineHeight - 3, Font14.lineHeight, Font14.lineHeight)];
        //_noticeLable.text = @"[通知]";
        
        [_messageConent setFrame:CGRectMake(3*INSETS+HEAD_SIZE + Font14.lineHeight, CELL_HEIGHT - (CELL_HEIGHT-HEAD_SIZE)/2 - Font14.lineHeight - 3, _bageView.frame.origin.x - 5 - 3*INSETS -HEAD_SIZE - Font14.lineHeight, Font14.lineHeight)];
        //        bug 6058
        _messageStatusimage.image=[UIImage imageNamed:@"icon-sending.png"];
    }else if([_strState isEqualToString:@"失败"])
    {
        [_messageStatusimage setFrame:CGRectMake(2*INSETS+HEAD_SIZE, CELL_HEIGHT - (CELL_HEIGHT-HEAD_SIZE)/2 - Font14.lineHeight - 3, Font14.lineHeight, Font14.lineHeight)];
        //_noticeLable.text = @"[通知]";
        
        [_messageConent setFrame:CGRectMake(3*INSETS+HEAD_SIZE + Font14.lineHeight, CELL_HEIGHT - (CELL_HEIGHT-HEAD_SIZE)/2 - Font14.lineHeight - 3, _bageView.frame.origin.x - 5 - 3*INSETS -HEAD_SIZE - Font14.lineHeight , Font14.lineHeight)];
        //        bug 6058 yh
        _messageStatusimage.image=[UIImage imageNamed:@"loginErrorImage"];
    }else{
        [_messageConent setFrame:CGRectMake(2*INSETS+HEAD_SIZE, CELL_HEIGHT - (CELL_HEIGHT-HEAD_SIZE)/2 - Font14.lineHeight - 3, _bageView.frame.origin.x - 5 - 2*INSETS -HEAD_SIZE, Font14.lineHeight)];
        [_messageStatusimage setFrame:CGRectZero];
        
    }
    self.longLine.frame = CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5);
    self.shortLine.frame = CGRectMake(15, self.frame.size.height - 0.5, self.frame.size.width - 15, 0.5);
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
//group bug 8677 fixed by lixu
-(void)setInfomation:(ChatMessageListCellData *)data

{
    [_userNickname setTextColor:[UIUtil colorWithHexString:@"#595757" withAlpha:1]];
    [_userNickname setText:[data senderName]];
    if (data.GroupId.length > 3) {
        CGroupModel *group = [[[CGroupMgr alloc]init]loadGroup:data.GroupId];
        [_userNickname setText:group.GroupName];
        if (group.messageSettingType == 0 || group.messageSettingType ==2) {
            _remindview.hidden = YES;
        }
        else
        {
            _remindview.hidden = NO;
        }
    }
    else
    {
        CContactModel *contact = [[[CContactMgr alloc]init] loadContactWithUserId:data.FriendUserId];
        if (contact.messageSettingType == 0 || contact.messageSettingType == 2) {
            _remindview.hidden = YES;
        }
        else
        {
            _remindview.hidden = NO;
        }
    }
    
    
    NSMutableAttributedString *attr = [data.content cookEmotionStringWithFont:Font14 textColor:GrayColor];
    if (data.messageType == SNSPMaterialTypeKMessage) {
        
        if (data.messageStatus == kMessageUserMute) {
            
            NSRange rage = [data.content rangeOfString:@"[草稿]"];
            [attr addAttributes:@{NSForegroundColorAttributeName:RedColor} range:rage];
            [_messageConent setAttributedText:attr];
        } else {
            
            _messageConent.attributedText = attr;
        }
        
    } else if (data.messageType == SNSPMaterialTypeKImage)
    {
        //        if (data.isRead)
        _messageConent.attributedText = attr;
        //        } else {
        //            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:data.content attributes:@{NSForegroundColorAttributeName:ContentLightGrayColor,
        //                                                                                                                          NSFontAttributeName:[UIFont systemFontOfSize:14]
        //                                                                                                                          }];
        //            NSRange rage = [data.content rangeOfString:@"[图片]"];
        //            if(rage.location != NSNotFound) {
        //                [attr addAttributes:@{NSForegroundColorAttributeName:RedColor} range:rage];
        //            }
        //            [_messageConent setAttributedText:attr];
        //
        //        }
        
    } else if (data.messageType == SNSPMaterialTypeKAudio) {
        //        if (data.isRead) {
        _messageConent.attributedText = attr;
        //        } else {
        //            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:data.content attributes:@{NSForegroundColorAttributeName:ContentLightGrayColor,
        //                                                                                                                          NSFontAttributeName:[UIFont systemFontOfSize:14]
        //                                                                                                                          }];
        //            NSRange rage = [data.content rangeOfString:@"[语音]"];
        //            if(rage.location != NSNotFound) {
        //                [attr addAttributes:@{NSForegroundColorAttributeName:RedColor} range:rage];
        //            }
        //            [_messageConent setAttributedText:attr];
        //
        //        }
        
    } else  if (data.messageType == SNSPMaterialNotice) {
        _noticeLable.text = @"[通知]";
    } else if (data.messageType == SNSPMaterialTypeKVideo){
      _messageConent.attributedText = attr;
    }else  if (data.messageType == SNSPMaterialTypeKFile) {
        //        if (data.isRead) {
        _messageConent.attributedText = attr;
        //            [_messageConent setTextColor:[UIColor lightGrayColor]];
        //        } else {
        //            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:data.content attributes:@{NSForegroundColorAttributeName:ContentLightGrayColor,
        //                                                                                                                          NSFontAttributeName:[UIFont systemFontOfSize:14]
        //                                                                                                                          }];
        //            NSRange rage = [data.content rangeOfString:@"[文件]"];
        //            if(rage.location != NSNotFound) {
        //                [attr addAttributes:@{NSForegroundColorAttributeName:RedColor} range:rage];
        //            }
        //            [_messageConent setAttributedText:attr];
        //
        //        }
        
    } else if (data.messageType == SNSRedPacket){
         _messageConent.attributedText = attr;
    }else{
        _noticeLable.text = @"";
    }
    
    [_timeLable setText:[data sendTime]];
    //    [_userHead setMultimediaFileUri:[data headImageUri] type:SNSPMaterialTypeKImage];
    if (data.GroupId.length > 3) {
        if (data.groupType) {
            [_userHead setBackgroundImage:[UIImage imageNamed:@"icon-discussList"] forState:UIControlStateNormal];
        } else {
            [_userHead setBackgroundImage:[UIImage imageNamed:@"xiaoxi_icon_group"] forState:UIControlStateNormal];
        }
        
    } else {
        if (![data.MineUserId isEqualToString:data.FriendUserId]) {
            if (data.isKefu) {
                [_userHead setBackgroundImage:[UIImage imageNamed:@"head-kefu"] forState:UIControlStateNormal];
            } else {
                [_userHead sd_setBackgroundImageWithURL:[NSURL URLWithString:data.headImageUri] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"110-110"]];
            }
        } else {
            [_userHead setBackgroundImage:[UIImage imageNamed:@"icon-iMAC"] forState:UIControlStateNormal];
        }
    }
    if ([data unreadNumber]==0) {
        [_bageView setHidden:YES];
        [_bageNumber setHidden:YES];
    }
    else
    {
        [_bageView setHidden:NO];
        [_bageNumber setHidden:NO];
        if ([data unreadNumber]>99) {
            [_bageNumber setText:@"99+"];
            [_bageView setImage:[UIImage imageNamed:@"back-remindRectangle"]];
            [self setNeedsLayout];
        }
        else
        {
            [_bageNumber setText:[NSString stringWithFormat:@"%zd",[data unreadNumber]]];
            [_bageView setImage:[UIImage imageNamed:@"tabbar_badge"]];
            [self setNeedsLayout];
        }
    }
    
    if (data.messageStatus==kMessageSending) {
        _strState= @"发送中";
        return;
    }else if(data.messageStatus==kMessageSendFailed){
        _strState = @"失败";
        return;
    }
    else{
        _strState = @"";
    }
}

-(void)hideBageView
{
    [_bageView setHidden:YES];
    [_bageNumber setHidden:YES];
    
}

//-(void)setHeadImage
//{
//    [_userHead setImage:[UIImage imageNamed:@"default_head.jpeg"]];
//}

@end
